import { get, post } from '@/utils/request.js'
import Config from '@/config'

export default {
  list(id, pageSize, pageIndex) {
    return post(Config.API_VERSION + '/TargetClients/Contact/QueryPages', {
      page: {
        pageRows: pageSize,
        pageIndex: pageIndex,
        total: 0
      },
      clientOrgId: id
    })
  },
  listDepartment(id) {
    return get(Config.API_VERSION + '/TargetClients/ContactDepartment/GetList', { clientOrgId: id })
  },
  detail(id) {
    return get(Config.API_VERSION + '/TargetClients/Contact/GetContact', { id: id })
  },
  detailDepartment(id) {
    return get(Config.API_VERSION + '/TargetClients/ContactDepartment/Get', { id: id })
  },
  update(data) {
    return post(Config.API_VERSION + '/TargetClients/Contact/UpdateContact', data)
  },
  updateDepartmentContact(data) {
    return post(Config.API_VERSION + '/TargetClients/ContactDepartment/Update', data)
  },
  add(data) {
    return post(Config.API_VERSION + '/TargetClients/Contact/AddContact', data)
  },
  addDepartmentContact(data) {
    return post(Config.API_VERSION + '/TargetClients/ContactDepartment/Add', data)
  },
  softDelete(contactId) {
    return post(Config.API_VERSION + '/TargetClients/Contact/UpdateContactStatus', {
      id: contactId,
      status: 1
    })
  },
  deleteDepartmentContact(id) {
    return get(Config.API_VERSION + '/TargetClients/ContactDepartment/Delete', { id: id })
  },
  isUniqueContactNameInThisClient(name, clientId, contactId) {
    const params = {
      Name: name,
      ClientOrgID: clientId
    }
    if (contactId) params.Id = contactId
    return get(Config.API_VERSION + '/TargetClients/Contact/NameIsExist', params)
  },
  async isUniqueEmailInThisClient(rule, email, clientId, contactId) {
    if (email) {
      const params = {
        Email: email,
        ClientOrgID: clientId
      }
      if (contactId) params.Id = contactId
      try {
        const res = await get(Config.API_VERSION + '/TargetClients/Contact/EmailIsExist', params)
        if (res.data === false) {
          return Promise.resolve()
        } else {
          return Promise.reject(new Error(''))
        }
      } catch (error) {
        return Promise.reject(new Error(''))
      }
    }
  }
}
